//Source file: D:\\test\\com\\ce\\pms\\custservice\\service\\impl\\ReturnVisitServiceImpl.java

package com.ce.pms.custservice.service.impl;
/**
 * @author 高嵩 编写日期:2008-12-16
 *
 */
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.springframework.util.Assert;

import com.ce.pms.base.vo.SearchVO;
import com.ce.pms.common.model.SysInfo;
import com.ce.pms.common.model.SysrefcodeDO;
import com.ce.pms.custservice.dao.ICustServiceEventDao;
import com.ce.pms.custservice.dao.IReturnVisitRecordDao;
import com.ce.pms.custservice.model.AbstractQuestionaire;
import com.ce.pms.custservice.model.CustServiceEventDO;
import com.ce.pms.custservice.model.QuestionAndAnswer;
import com.ce.pms.custservice.model.ReturnVisitRecordDO;
import com.ce.pms.custservice.service.ICustServChargeService;
import com.ce.pms.custservice.service.IEventManageService;
import com.ce.pms.custservice.service.IReturnVisitService;
import com.ce.pms.custservice.vo.AbstractQuestionaireVO;
import com.ce.pms.custservice.vo.QuestionAndAnswerVO;
import com.ce.pms.custservice.vo.ReturnVisitRecordVO;
import com.ce.pms.exception.BusinessException;
import com.ce.pms.house.model.HouseDomain;
import com.ce.pms.system.custservice.model.ReturnVisitItemDO;
import com.ce.pms.system.custservice.model.ReturnVisitWayDO;
import com.ce.util.BeanUtil;
import com.ce.util.BlankUtil;
import com.sitechasia.webx.core.support.Page;
/**
 *
 * 	名称: com.ce.pms.custservice.service.impl.ReturnVisitServiceImpl <br>
 *  回访管理业务类
 *
 * @author  高嵩  <br>
 * Created on： 2008-12-22 <br>
 * @author <br>
 * update on： <br>
 * why: <br>
 */
public class ReturnVisitServiceImpl extends QuestionaireServiceImpl implements IReturnVisitService {


	private ICustServChargeService custServChargeSevice;

	private ICustServiceEventDao custServiceEventDao;

	/**
	 * @return the custServiceEventDao
	 */
	public ICustServiceEventDao getCustServiceEventDao() {
		return custServiceEventDao;
	}



	/**
	 * @param custServiceEventDao the custServiceEventDao to set
	 */
	public void setCustServiceEventDao(ICustServiceEventDao custServiceEventDao) {
		this.custServiceEventDao = custServiceEventDao;
	}



	/**
	 * @return the custServChargeSevice
	 */
	public ICustServChargeService getCustServChargeSevice() {
		return custServChargeSevice;
	}



	/**
	 * @param custServChargeSevice the custServChargeSevice to set
	 */
	public void setCustServChargeSevice(ICustServChargeService custServChargeSevice) {
		this.custServChargeSevice = custServChargeSevice;
	}



	/* (non-Javadoc)
	 * @see com.ce.pms.custservice.service.impl.QuestionaireServiceImpl#addQuestionair(java.lang.Long, com.ce.pms.custservice.vo.AbstractQuestionaireVO)
	 */
	   /**
	    * 功能描述: 新增回访  <br>
	    * @param csEventID - 事件ID
	    * @param AbstractQuestionaireVO
	    * Create author:高嵩 <br>
	    * Create on:2008-12-23  <br>
	    * Edit author:  <br>
	    * Edit on:    <br>
	    * Why:     <br>
	    */
	@Override
	public void addQuestionair(Long csEventID, AbstractQuestionaireVO questionaireVO) {
		// TODO Auto-generated method stub
		Assert.notNull(csEventID,"customerEvent ID notNull");
		Assert.notNull(questionaireVO);

		//Assert.notEmpty(questionaireVO.getQuestionAndAnswerSet());


		ReturnVisitRecordDO returnVisitRecordDO = new ReturnVisitRecordDO();
		this.assembleV2D(questionaireVO, returnVisitRecordDO);

			for(QuestionAndAnswerVO questionAndAnswerVO : questionaireVO.getQuestionAndAnswerSet())
			{
				QuestionAndAnswer questionAndAnswer = new QuestionAndAnswer();
				this.assembleV2V(questionAndAnswerVO, questionAndAnswer);
				questionAndAnswer.setAbstraQuestionaire(returnVisitRecordDO);
				returnVisitRecordDO.addAnswer(questionAndAnswer);

			}
			this.getReturnVisitRecordDao().saveObject(returnVisitRecordDO);
			this.getEventManageService().noteAddReturnVisit(csEventID);

	}



	/* (non-Javadoc)
	 * @see com.ce.pms.custservice.service.impl.QuestionaireServiceImpl#setAside(java.lang.Long)
	 */
		/**
	    * 功能描述: 作废访问登记  <br>
	    * @param AbstractQuestionaireVO
	    * Create author:高嵩<br>
	    * Create on:2008-12-16  <br>
	    * Edit author:  <br>
	    * Edit on:    <br>
	    * Why:     <br>
	    */

	@Override
	public void setAside(AbstractQuestionaireVO questionaireVO) {
		// TODO Auto-generated method stub
		Assert.notNull(questionaireVO);
		//Assert.notNull(questionaireVO.getCustomerServiceEventID(),"custEventID notNull");
		Assert.notNull(questionaireVO.getKeyword(),"returnID not null");//回访问卷ID
		ReturnVisitRecordDO returnVisitRecordDO =  this.getReturnVisitRecordDao().findById(questionaireVO.getKeyword());
		if(returnVisitRecordDO==null){
			throw new BusinessException("custservice:returnVisitNotExistsException",BusinessException.WARNING);
		}

		returnVisitRecordDO.setValidityFalse();
		returnVisitRecordDO.setRemark(questionaireVO.getRemark());
		setReturnVisitStatus(questionaireVO.getCustomerServiceEventID());
		this.getReturnVisitRecordDao().updateObject(returnVisitRecordDO);

	}
	/***
	 * 功能描述:  作废回访时判断是否要改回访状态
	 * 如果回访记录只有一条，作废时就把回访状态改为待回访
	 * 如果回访记录里有一条没作废的回访记录时，不改回访状态<br>
	 *
	 * Create author:高嵩 <br>
	 * Create on:2009-1-13  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public static final String RETURN_STATUS_CODE = "1";
	public void setReturnVisitStatus(Long csEventId){
		boolean flag = false;
		List<ReturnVisitRecordDO> returnVisitRecordDOlist = this.returnVisitRecordDao.queryReturnVisitRecordByEventID(csEventId);
		for(ReturnVisitRecordDO returnVisitRecordDO : returnVisitRecordDOlist){

			if(RETURN_STATUS_CODE.equals(returnVisitRecordDO.getValidity())){
				flag = true;
				break;
			}
		}
		if(!flag)
		custServiceEventDao.findById(csEventId).setReturnVisitStatuz(CustServiceEventDO.RETURN_VISIT_STATUZ_PREPARE_CODE);
	}
	/***
	 * 功能描述:  判断是否允许作废回访
	 * @param   回访问卷ID
	 * @return
	 * Create author:高嵩 <br>
	 * Create on:2009-1-6  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public boolean isAllowSetAsideReturnVisit(Long returnVisitID){
		Assert.notNull(returnVisitID,"returnID not null");//回访问卷ID
		ReturnVisitRecordDO returnVisitRecordDO =  this.getReturnVisitRecordDao().findById(returnVisitID);
		if(returnVisitRecordDO==null){
			throw new BusinessException("custservice:returnVisitNotExistsException",BusinessException.WARNING);
		}
		//如果回访状态为"作废"，则不能进行作废回访
		if(returnVisitRecordDO.getValidity().equals(SysrefcodeDO.CONSTANTS_DIGITAL_ZERO)){
			throw new BusinessException("custservice:customerEvent_Already_SetAsideException",BusinessException.ERROR);
		}
		Long csEventID = returnVisitRecordDO.getCustomerServiceEventDO().getKeyword();
		Assert.notNull(csEventID,"customerEvent ID notNull");
		return this.eventManageService.isAllowSetAsideReturnVisit(csEventID);
	}

	/**
	    * 功能描述: 根据ID查询回访登记信息  <br>
	    * @return ReturnVisitRecordVO
	    * @param id 事件ID
	    * Create author:高嵩<br>
	    * @roseuid 4943511A032C
	    * Create on:2008-12-16  <br>
	    * Edit author:  <br>
	    * Edit on:    <br>
	    * Why:     <br>
	    */
   public ReturnVisitRecordVO getReturnVisitRecordByID(Long id)
   {
	   Assert.notNull(id);
	   ReturnVisitRecordDO returnVisitRecordDO = this.getReturnVisitRecordDao().findById(id);
	   if(BlankUtil.isBlank(returnVisitRecordDO)){
			throw new BusinessException("returnVisitNotExistsException",BusinessException.WARNING);
		}
	   ReturnVisitRecordVO returnVisitRecordVO = new ReturnVisitRecordVO();
	   this.assembleD2V(returnVisitRecordDO, returnVisitRecordVO);
	   return returnVisitRecordVO;
   }

   /**
    * 查询回访登记列表(暂不实现)
    * @param searchVO
    * @return List<ReturnVisitRecordVO>
    * Create author:高嵩<br>
    * @roseuid 4943515100BB
    * Edit author:  <br>
	* Edit on:    <br>
	* Why:     <br>
	*/
   public List<ReturnVisitRecordVO> queryReturnVisitRecordList(SearchVO searchVO)
   {
	   return null;
   }

   /**
    * 分页查询回访登记
    * @param search
    * @return Page
    * @roseuid 49435189006D
    * Create author:高嵩<br>
    * Edit author:  <br>
	* Edit on:    <br>
	* Why:     <br>
	*/
   public Page queryReturnVisitRecordByPage(ReturnVisitRecordVO returnVisitRecordVO)
   {
	   Assert.notNull(returnVisitRecordVO);
	   Page page = this.getReturnVisitRecordDao().queryReturnVisitRecordByPage(returnVisitRecordVO);
	   List<ReturnVisitRecordDO> returnVisitRecordDOList = page.getResult();

	   List<ReturnVisitRecordVO> returnVisitRecordVOList = returnVisitRecordDOToVO(returnVisitRecordDOList);
	   page.setResult(returnVisitRecordVOList);
	   return page;
   }


	private void assembleV2V(QuestionAndAnswerVO questionAndAnswerVO,
			QuestionAndAnswer questionAndAnswer) {
		if(null!=questionAndAnswerVO.getAnswer() && !"".equals(questionAndAnswerVO.getAnswer())){
			questionAndAnswer.setAnswer(questionAndAnswerVO.getAnswer());
		}
		if(null!=questionAndAnswerVO.getRemark() && !"".equals(questionAndAnswerVO.getRemark())){
			questionAndAnswer.setRemark(questionAndAnswerVO.getRemark());
		}
		if(null!=questionAndAnswerVO.getReturnVisitItemID() && !"".equals(questionAndAnswerVO.getReturnVisitItemID())){
			questionAndAnswer.setReturnVisitItemDO(new ReturnVisitItemDO(questionAndAnswerVO.getReturnVisitItemID()));
		}

	}
   /**
	 * 功能描述:批量将Domain转换为VO<br>
	 * @param domainlist
	 * @return
	 * Create author:高嵩<br>
	 * Create on:2008-10-7  <br>
	 */
	private List<ReturnVisitRecordVO> returnVisitRecordDOToVO(List<ReturnVisitRecordDO> domainlist){
		if(domainlist == null || domainlist.size() == 0) return null;
		List<ReturnVisitRecordVO> voList = new ArrayList<ReturnVisitRecordVO>();
		for(Iterator<ReturnVisitRecordDO> iterator = domainlist.iterator(); iterator.hasNext();){
			voList.add(assembleD2V(iterator.next(), new ReturnVisitRecordVO()));
		}
		return voList;
	}

   /**
    * @param rvRecordVO
    * @param rvRecordDO
    * @roseuid 4933B78A00CB
    *
    */
   public ReturnVisitRecordDO assembleV2D(AbstractQuestionaireVO rvRecordVO, ReturnVisitRecordDO rvRecordDO)
   {
	  // Assert.isTrue(PopulateUtil.populate(rvRecordVO, rvRecordDO));
	   //BeanUtil.convertVoAsDo(rvRecordVO, rvRecordDO);
	   rvRecordDO.setClient(rvRecordVO.getClient());

	   if(null!=rvRecordVO.getHouseID()&&!"".equals(rvRecordVO.getHouseID())){
		   rvRecordDO.setHouse(new HouseDomain(rvRecordVO.getHouseID()));
	   }
	   if(null!=rvRecordVO.getHouseNumber()&&!"".equals(rvRecordVO.getHouseNumber())){
		   rvRecordDO.setHouseNumber(rvRecordVO.getHouseNumber());
	   }
	   Assert.notNull(rvRecordVO.getRecorder());
	   rvRecordDO.setRecorder(rvRecordVO.getRecorder());
	   rvRecordDO.setRecordTime(rvRecordVO.getRecordTime());
	   if(null!=rvRecordVO.getSummarize() && !"".equals(rvRecordVO.getSummarize())){
		   rvRecordDO.setSummarize(rvRecordVO.getSummarize());
	   }
	   if(null!=rvRecordVO.getReturnVisitWayID() && !"".equals(rvRecordVO.getReturnVisitWayID())){
		   rvRecordDO.setReturnVisitWayDO(new ReturnVisitWayDO(rvRecordVO.getReturnVisitWayID()));
	   }



	   rvRecordDO.setCustomerServiceEventDO(new CustServiceEventDO(rvRecordVO.getCustomerServiceEventID()));
	   Assert.notNull(rvRecordVO.getCreator());
	   rvRecordDO.setSysInfo(new SysInfo(rvRecordVO.getCreator(),this.getReturnVisitRecordDao().getDate()));

	   return rvRecordDO;
   }



   /**
    * @param rvRecordDO
    * @param rvRecordVO
    * @return Void
    * @roseuid 4933B7E502DE
    */
   public ReturnVisitRecordVO assembleD2V(AbstractQuestionaire rvRecordDO, ReturnVisitRecordVO rvRecordVO)
   {
	   BeanUtil.convertVoAsDo(rvRecordDO, rvRecordVO);
	   if(!BlankUtil.isBlank(rvRecordDO.getReturnVisitWayDO())){
		   ReturnVisitWayDO returnVisitWay = rvRecordDO.getReturnVisitWayDO();
		   rvRecordVO.setReturnVisitWayID(returnVisitWay.getKeyword());
		   rvRecordVO.setReturnVisitWayName(returnVisitWay.getName());
	   }
	   if(!BlankUtil.isBlank(rvRecordDO.getCustomerServiceEventDO())){
		   rvRecordVO.setCustomerServiceEventID(rvRecordDO.getCustomerServiceEventDO().getKeyword());
		   rvRecordVO.setEventNo(rvRecordDO.getCustomerServiceEventDO().getEventNo());
		   rvRecordVO.setFullAddress(rvRecordDO.getCustomerServiceEventDO().getFullAddress());
		   rvRecordVO.setClient(rvRecordDO.getClient());
	   }
	  if(!BlankUtil.isBlank(rvRecordDO.getHouse())){
		  rvRecordVO.setHouseID(rvRecordDO.getHouse().getKeyword());
		  rvRecordVO.setHouseNumber(rvRecordDO.getHouseNumber());
	   }

	  if(null!=rvRecordDO.getQuestionAndAnswerSet() && rvRecordDO.getQuestionAndAnswerSet().size()!=0){
		  Set<QuestionAndAnswerVO> questionAndAnswerSet = new HashSet<QuestionAndAnswerVO>();
		  for(QuestionAndAnswer questionAndAnswer : rvRecordDO.getQuestionAndAnswerSet())
			{
			  QuestionAndAnswerVO questionAndAnswerVO = new QuestionAndAnswerVO();
			  if(null!=questionAndAnswer.getAnswer() && !"".equals(questionAndAnswer.getAnswer())){
				  questionAndAnswerVO.setAnswer(questionAndAnswer.getAnswer());
				}
				if(null!=questionAndAnswer.getRemark() && !"".equals(questionAndAnswer.getRemark())){
					questionAndAnswerVO.setRemark(questionAndAnswer.getRemark());
				}
				if(null!=questionAndAnswer.getReturnVisitItemDO().getKeyword() && !"".equals(questionAndAnswer.getReturnVisitItemDO().getKeyword())){
					questionAndAnswerVO.setReturnVisitItemID(questionAndAnswer.getReturnVisitItemDO().getKeyword());
				}
				questionAndAnswerSet.add(questionAndAnswerVO);

			}
		  rvRecordVO.setQuestionAndAnswerSet(questionAndAnswerSet);

	  }

	   return rvRecordVO;
   }

    private IEventManageService eventManageService;

	private IReturnVisitRecordDao returnVisitRecordDao;

	/**
	    * @roseuid 49436CB103D8
	    */
	   public ReturnVisitServiceImpl()
	   {

	   }
  /**
	 * @return the returnVisitRecordDao
	 */
	public IReturnVisitRecordDao getReturnVisitRecordDao() {
		return returnVisitRecordDao;
	}

	/**
	 * @param returnVisitRecordDao the returnVisitRecordDao to set
	 */
	public void setReturnVisitRecordDao(IReturnVisitRecordDao returnVisitRecordDao) {
		this.returnVisitRecordDao = returnVisitRecordDao;
	}

/**
	 * @return the eventManageService
	 */
	public IEventManageService getEventManageService() {
		return eventManageService;
	}

	/**
	 * @param eventManageService the eventManageService to set
	 */
	public void setEventManageService(IEventManageService eventManageService) {
		this.eventManageService = eventManageService;
	}

}
